package com.gxa.p2p.service.impl;

import com.gxa.p2p.mapper.AdminMapper;
import com.gxa.p2p.mapper.MarkMapper;
import com.gxa.p2p.pojo.Admin;
import com.gxa.p2p.service.AdminService;
import com.gxa.p2p.service.MarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.*;

@Transactional(rollbackFor = {Throwable.class})
@Service("adminService")
public class AdminServiceImpl implements AdminService {

    @Autowired
    private MarkMapper markMapper;

    Timestamp preDay;

    Timestamp nextDay;

    @Autowired
    private AdminMapper mapper=null;

    @Autowired
    MarkService markService;

    @Override
    public Admin login(String phone) {
        return mapper.login(phone);
    }

    @Override
    public Map totalUser() {
        HashMap<String, Object> totalUser = new HashMap<>();
        //所有用户数
        int allUser = mapper.findAllUser().size();
        //昨日用户数  刷新时间
        init();
        int yesterdayUser = mapper.findUserYesterday(preDay, nextDay).size();
        totalUser.put("totalUser",allUser);
        totalUser.put("yesterdayUser",yesterdayUser);

        return totalUser;
    }

    @Override
    public Map totalInvset() {
        HashMap<String, Object> totalInvset = new HashMap<>();
        //所有投资
        Double allInvest = mapper.findAllInvest();
        //昨日投资
        init();
        Double investYestday = mapper.findInvestYestday(preDay, nextDay);
        totalInvset.put("allInvest",allInvest);
        totalInvset.put("investYestday",investYestday);
        return totalInvset;
    }

    @Override
    public Map totalProfit() {
        HashMap<String, Object> totalProfit = new HashMap<>();
        //所有收益
        Double allProfit = mapper.findAllProfit();
        //昨天投资
        init();
        Double profitYesterday = mapper.findProfitYesterday(preDay, nextDay);
        totalProfit.put("allProfit",allProfit);
        totalProfit.put("profitYesterday",profitYesterday);
        return totalProfit;
    }

    @Override
    public Map totalToDoList() {
        HashMap<String, Object> totalToDoList = new HashMap<>();
        Integer newMark = markMapper.countByState("新标待审");
        Integer needSjMark = markMapper.countByState("待上架");
        Integer fullMark = markMapper.countByState("满标待审");
        //今日待还款人数
        Integer backAll = markService.todayBackMoney();
        Integer recordWaitCheck = markMapper.findByStateToRecord("待审").size();
        Integer recordDo = markMapper.findRecordByState("处理中");
        totalToDoList.put("newMark",newMark);
        totalToDoList.put("needSjMark",needSjMark);
        totalToDoList.put("fullMark",fullMark);
        totalToDoList.put("backAll",backAll);
        totalToDoList.put("recordWaitCheck",recordWaitCheck);
        totalToDoList.put("recordDo",recordDo);
        return totalToDoList;
    }

    @Override
    public List<Double> totalInvestNearMonth() {
        return markService.totalInvestNearMonth();
    }

    @Override
    public Map<String, List<Double>> totalOutAndInputMoney() {
        ArrayList<Double> doublesC = new ArrayList<>();
        ArrayList<Double> doublesR = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH,-7);
        Timestamp start = new Timestamp(calendar.getTimeInMillis());
        for(int i=0;i<7;i++){
            calendar.add(Calendar.DAY_OF_MONTH,1);
            Timestamp end = new Timestamp(calendar.getTimeInMillis());
            Double countC = mapper.findPlatformByTime("出账", start, end);
            if(countC==null){
                countC = 0.0;
            }
            doublesC.add(countC);
            Double countR = mapper.findPlatformByTime("入账", start, end);
            if(countR==null){
                countR = 0.0;
            }
            doublesR.add(countR);
            start = end;
        }
        HashMap<String, List<Double>> stringListHashMap = new HashMap<>();
        stringListHashMap.put("countOut",doublesC);
        stringListHashMap.put("countInput",doublesR);
        return stringListHashMap;
    }


    @Override
    public Map totalBorrow(){
        return markService.totalBorrow();
    }


    private void init(){
        Calendar now = Calendar.getInstance();
        //获取前一天的开始时间
        now.add(Calendar.DATE,-1);
        now.set(now.get(Calendar.YEAR),now.get(Calendar.MONTH),now.get(Calendar.DAY_OF_MONTH),0,0,0);
        preDay = new Timestamp(now.getTimeInMillis());


        now.add(Calendar.DATE,1);
        now.set(now.get(Calendar.YEAR),now.get(Calendar.MONTH),now.get(Calendar.DAY_OF_MONTH),0,0,0);
        nextDay = new Timestamp(now.getTimeInMillis());
    }
}
